﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BepInEx;
using HarmonyLib;
using BepInEx.Logging;
using BepInEx.Configuration;


namespace DSPMod {
	[BepInPlugin(GUID, NAME, VERSION)]
	[BepInProcess(GAME_PROCESS)]

	public class Plugin : BaseUnityPlugin {
		public const string GUID = "org.lhq.dsp";
		public const string NAME = "DSPMod";
		public const string VERSION = "1.0";
		private const string GAME_PROCESS = "DSPGAME.exe";



		private static ConfigEntry<int> times;
		private static ConfigEntry<long> SolarPanelRatio;
		private static ConfigEntry<long> WindTurbineRatio;
		private static ConfigEntry<long> ThermalPowerRatio;
		private static ConfigEntry<long> FusionPowerRatio;
		private static ConfigEntry<long> RayReceiverRatio;
		private static ConfigEntry<long> ArtificialStarRatio;


		void Start()
		{
			Logger.LogInfo("我王某人的第一个戴森球 Mod");
			_OnStart();
			_Trash();


		}
		void Update()
		{

		}
		void _Trash()
		{
			Harmony.CreateAndPatchAll(typeof(Plugin), null);
			times = Config.Bind<int>("config", "times", 100, "销毁每个物品获得沙土的数量");

		}
		[HarmonyPrefix]
		[HarmonyPatch(typeof(StorageComponent), "AddItem",new Type[] { typeof(int),typeof(int),typeof(bool) })]
		public static bool DoTrash(ref int __result,StorageComponent __instance ,int itemId,int count, bool useBan = false)
        {
			if(useBan && __instance.size == __instance.bans && itemId != 0 && count != 0)
            {
				int num = count * times.Value;
				GameMain.mainPlayer.SetSandCount(GameMain.mainPlayer.sandCount + num);
				__result = 1;
				return false;
            }
            else
            {
				__result = 0;
				return true;
            }
        }
		void _OnStart()
        {
            Logger.LogInfo("Mod_Version:"+VERSION);
            Logger.LogInfo("Mod_Name:" + NAME);
            Logger.LogInfo("Game_Version:"+GameConfig.gameVersion);
            Logger.LogInfo("Game_Version:"+GameConfig.gameName);
            
            SolarPanelRatio = Config.Bind<long>("config", "SolarPanelRatio", 100, "太阳能板倍数");
            WindTurbineRatio = Config.Bind<long>("config", "WindTurbineRatio", 100, "风力发电倍数");
            ThermalPowerRatio = Config.Bind<long>("config", "ThermalPowerRatio", 10, "火力发电倍数");
            FusionPowerRatio = Config.Bind<long>("config", "FusionPowerRatio", 10, "核电倍数");
            RayReceiverRatio = Config.Bind<long>("config", "RayReceiverRatio", 10, "射线接收器倍数");
            ArtificialStarRatio = Config.Bind<long>("config", "ArtificialStarRatio", 10, "小太阳倍数");
            Harmony.CreateAndPatchAll(typeof(Plugin));
        }
		[HarmonyPrefix]
		[HarmonyPatch(typeof(PowerSystem), "NewGeneratorComponent")]
		private static bool NewGeneratorComponent_Prefix(ref long __state, ref int __result, int entityId, PrefabDesc desc)
		{

			if (desc.photovoltaic)
			{
	
				__state = SolarPanelRatio.Value;
			}
			else if (desc.windForcedPower)
			{
		
				__state = WindTurbineRatio.Value;
			}
			else if (desc.gammaRayReceiver)
			{
		
				__state = RayReceiverRatio.Value;

			}
			else if (desc.modelIndex == 118)
			{
				
				__state = FusionPowerRatio.Value;
			}
			else if (desc.modelIndex == 54)
			{
		
				__state = ThermalPowerRatio.Value;
			}
			else if (desc.modelIndex == 56)
			{
			
				__state = ArtificialStarRatio.Value;
			}
			else
			{
				__state = (long)1;
			}
			return true;

		}



		[HarmonyPostfix]
		[HarmonyPatch(typeof(PowerSystem), "NewGeneratorComponent")]
		private static void NewGeneratorComponent_Postfix(PowerSystem __instance, int __result, ref long __state)
		{
			PowerGeneratorComponent[] genPool = __instance.genPool;
			genPool[__result].genEnergyPerTick = genPool[__result].genEnergyPerTick * __state;
		}
	}
}
